package com.gxdf.uc.data;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GxdfEditText extends GxdfField {
	
	TextView label;
	EditText editText;
	boolean isValid;
	

	public GxdfEditText(Context context, Field field) {
		super(context, field);		
		this.setOrientation(LinearLayout.VERTICAL);
		label= new TextView(context);
		label.setText(this.field.getLabelValue());
		editText = new EditText(context);
		editText.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
		editText.setText(this.field.getValues().get(0));
		
		
		setDomainType();
		
		
	}

	private void setDomainType() {
		
		switch(field.getType().getTypeDomain()){
		
		case EMAIL : 
			editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
			break;
		case NUMBER:
			editText.setInputType(InputType.TYPE_CLASS_NUMBER);
			break;
		case DATE:
			editText.setInputType(InputType.TYPE_CLASS_DATETIME);
		default:
			break;
		
		}
		
	}

	public void showlabel(boolean v){		
		label.setEnabled(v);
	}


	@Override
	public String getValue() {		
		return this.editText.getText().toString();		
	}


	@Override
	public void drawField() {
		// TODO Auto-generated method stub
		this.addView(label);
		this.addView(editText);	
	}
	
	public void saveValues(){		
		
		if(!editText.getText().toString().isEmpty()){
		
			List<String> newvalues = new ArrayList<String>();
			newvalues.add(0, editText.getText().toString());
			actualValues.clear();
			
			actualValues=newvalues;
		}
		else{
			actualValues.clear();
		}
		
	}
	
	public boolean isModified(){
		return actualValues.equals(getField().getValues());
	}
	
	public List<String> getActualValues(){
		return this.actualValues;
	}

	@Override
	protected boolean errorFormat() {
		boolean invalid=true;
		
		if(getField().getFormatValidation().isEmpty()){
			invalid=false;
		}
		else{		
			//TODO: Ejecutar regexp. Si falla return true else return false	
			
			pattern = Pattern.compile(field.getFormatValidation());
			matcher = pattern.matcher(actualValues.get(0));
			invalid = matcher.matches();
		}
		
		return invalid;
	}

	@Override
	protected boolean errorLength() {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean isFieldvalid() {		
		return isValid;
	}

	@Override
	public void runValidationRules() {			
		if(errorRequired()){
			isValid=false;
			ValidationResult vr = new ValidationResult(field.getName(),ValidationType.REQUIRED, ValidationMessage.getMessage(ValidationType.REQUIRED));
			validRules.add(vr);
		}
		
		if(errorFormat()){
			isValid=false;
			ValidationResult vr = new ValidationResult(field.getName(),ValidationType.FORMAT, ValidationMessage.getMessage(ValidationType.FORMAT));
			validRules.add(vr);			
		}
		
		
	}


}
